package org.duanyu.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.duanyu.shopping_common.pojo.ProductType;
import org.duanyu.shopping_common.result.BusException;
import org.duanyu.shopping_common.result.CodeEnum;
import org.duanyu.shopping_common.service.ProductTypeService;
import org.duanyu.shopping_goods_service.mapper.ProductTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public void add(ProductType productType) {
        //根据父类型id查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getParentId());
        //根据父类型的级别。来设置当前类型的级别
        if (productTypeParent == null) {
            //如果没有父类型，则为1级类型
            productType.setLevel(1);
        } else if (productTypeParent.getLevel() < 3) {
            productType.setLevel(productTypeParent.getLevel() + 1);
        } else if (productTypeParent.getLevel() == 3) {
            throw new BusException(CodeEnum.INSERR_PRODUCT_TYPE);
        }
        productTypeMapper.insert(productType);

    }

    @Override
    public void update(ProductType productType) {
        //根据父类型id查询父类型
        ProductType parent = productTypeMapper.selectById(productType.getParentId());
        //根据父类型的级别。来设置当前类型的级别
        if (parent== null) {
            productType.setLevel(1);
        } else if (parent.getLevel() < 3) {
            productType.setLevel(parent.getLevel() + 1);
        } else if (parent.getLevel() == 3) {
            throw new BusException(CodeEnum.INSERR_PRODUCT_TYPE);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public void delete(Long id) {
        //查询该类型的子类型
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId", id);
        List<ProductType> productTypes = productTypeMapper.selectList(wrapper);
        //如果该类型有子类型，删除失败
        if (!productTypes.isEmpty()) {
            throw new BusException(CodeEnum.DELETE_PRODUCT_TYPE);
        }
        productTypeMapper.deleteById(id);
    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    /**
     * 分页查询商品类型
     * @param productType
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ProductType> search(ProductType productType, int page, int size) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        if (productType != null) {
            //类型名不为空时
            if (StringUtils.hasText(productType.getName())) {
                wrapper.like("name", productType.getName());
            }
            //上级类型id不为空时
            if(productType.getParentId()!=null){
                wrapper.eq("parentId", productType.getParentId());
            }
        }
        return productTypeMapper.selectPage(new Page<>(page, size), wrapper);
    }


    //根据条件查询商品类型
    @Override
    public List<ProductType> findProductType(ProductType productType) {
        QueryWrapper<ProductType> wrapper = new QueryWrapper<>();
        if (productType != null) {
            //类型名不为空时
            if (StringUtils.hasText(productType.getName())) {
                wrapper.like("name", productType.getName());
            }
            //上级类型id不为空时
            if(productType.getParentId()!=null){
                wrapper.eq("parentId", productType.getParentId());
            }
        }
        return productTypeMapper.selectList(wrapper);
    }
}
